<script setup>
  import Cookies from 'js-cookie';
  import router from "@/apps/client/router.js";
  import {auth} from "@/apps/client/utils/userApi.js";
  const token = Cookies.get("token")
  const localToken = localStorage.getItem('token');
  // alert("响应: " + token + "  本地: " + localToken)
  //
  if (token){
    // alert("登陆成功cookie: " + token)
    localStorage.setItem('token', token)
    Cookies.remove("token")

    router.push({name: "home"});
  } else if (localToken) {
    //alert("历史的cookie: " + localToken)
    auth().then((body) => {
      if (body["code"] !== 200){
        //alert("验证失败: " + body["msg"])
        return toAuth();
      }
      //alert("验证成功: " + http.status)
      router.push({name: "home"});
    }).catch((error) => {
      if (error.status === 401){
        localStorage.removeItem('token')
        //alert("验证失败")
        return toAuth();
      }
    })

  } else {
    toAuth();
  }
  function toAuth(){
    const corpid = 'wxe5912ad2bcf60f56';
    const redirectUri = encodeURIComponent('https://gwsysyy.sxmu.edu.cn/SchoolVisitors/api/v1/user/weChatAuth');
    const agentid = '1000262';
    const state = 'STATE';

    const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${corpid}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_privateinfo&agentid=${agentid}&state=${state}#wechat_redirect`;

    window.location.href = url;
   // window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwdaa74fc85f3e1bcd&redirect_uri=https://schoolvisitors.idce.top:10443/api/v1/user/weChatAuth&response_type=code&scope=snsapi_base&appid=wwdaa74fc85f3e1bcd&agentid=1000002&state=STATE#wechat_redirect";

  }
</script>

<template>
  <van-loading type="spinner" />
</template>

<style scoped>

</style>